package com.dzeshko;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.dzeshko.auth.GplusAuthHelper;
import com.dzeshko.content.FeedContentManager;

import extent.ParamCallback;

public class AuthActivity extends Activity {

	WebView mWebView;
	ProgressBar mPrBar;
	private JSONObject jsonWithToken;
	SharedPreferences authPrefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auth);
		Log.d("AuthActivity", "oncreate");
		mPrBar = (ProgressBar) findViewById(R.id.progressBar);
		mWebView = (WebView) findViewById(R.id.webView);
		mWebView.getSettings().setJavaScriptEnabled(true);

		mWebView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(final WebView view, String url) {
				if (Uri.parse(url).getLastPathSegment().equals("approval")) {
					view.setVisibility(View.INVISIBLE);
					GplusAuthHelper.setAuthCode(view.getTitle());
					Log.d("Step1:authcode",GplusAuthHelper.getAuthCode());
					
					FeedContentManager.getInstance().getToken(new ParamCallback<JSONObject>() {

								@Override
								public void onSuccess(JSONObject p) {
									// TODO Auto-generated method stub
									jsonWithToken = p;
									authPrefs = getSharedPreferences(GplusAuthHelper.AUTH_PREFS, Context.MODE_PRIVATE);
									Editor edit = authPrefs.edit();
									if (jsonWithToken != null) {
										try {
											edit.putString(GplusAuthHelper.ACCESS_TOKEN,jsonWithToken.getString(GplusAuthHelper.ACCESS_TOKEN));
										} catch (JSONException e1) {
											// TODO Auto-generated catch block
											e1.printStackTrace();
											Log.e("JSONexcept", e1.getMessage());
										}
										edit.commit();
										Log.i("showprefs", authPrefs.getString(GplusAuthHelper.ACCESS_TOKEN, ""));

										setResult(RESULT_OK);
										finish();
									} else {
										setResult(RESULT_CANCELED);
										Toast.makeText(AuthActivity.this,"Signing in did not completed :(",Toast.LENGTH_LONG).show();
										finish();
									}
								}

								@Override
								public void onError(Throwable e) {
									// TODO Auto-generated method stub
									setResult(RESULT_CANCELED);
									Toast.makeText(AuthActivity.this,
											"Signing in did not completed :(",
											Toast.LENGTH_LONG).show();
									finish();
								}
							});
					
				}
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				Log.i("URL", Uri.parse(url).toString());
				if (Uri.parse(url).getLastPathSegment().equals("approval")) {
					view.setVisibility(View.INVISIBLE);
					Log.i("VISIBLE", "TRY");
					ProgressBar loading = (ProgressBar) findViewById(R.id.progressBar);
					loading.setVisibility(View.VISIBLE);
					Log.i("VISIBLE", "OK");
				}
			}
		});
		mWebView.loadUrl(GplusAuthHelper.getResponseUrl());

	}

}
